<template>
  <div class="home">
    <h1>商品信息</h1>
    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="id" label="id" width="180"> </el-table-column>
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column prop="count" label="数量">
        <template slot-scope="scope">
          {{ scope.row.count > 0 ? scope.row.count : "" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <template v-if="scope.row.count > 0">
            <el-button @click="add(scope.row.id)">+</el-button>
            <el-button @click="plus(scope.row.id)">-</el-button>
          </template>

          <el-button v-else @click="add(scope.row.id)">加入购物车</el-button>
        </template>
      </el-table-column>
    </el-table>

    <h1>购物车信息</h1>
    <el-table :data="shopcarList" border style="width: 100%">
      <el-table-column prop="id" label="id" width="180"> </el-table-column>
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column label="总价">
        <template slot-scope="scope">
          <div>
            {{ scope.row.count * scope.row.price }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="count" label="数量">
        <template slot-scope="scope">
          {{ scope.row.count > 0 ? scope.row.count : "" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <template>
            <el-button @click="add(scope.row.id)">+</el-button>
            <el-button @click="plus(scope.row.id)">-</el-button>
            <el-button @click="remove(scope.row.id)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!-- 计算总价和总数 全部删除 -->
    <!-- 为什么加了持久化刷新还是回去 -->
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  methods: {
    ...mapMutations(["add", "plus", "remove"]),
  },
  created() {
    this.$store.dispatch("getData");
    //调用action请求初始数据
  },
  computed: {
    list() {
      return this.$store.state.list;
    },
    //拿到vuex里的list数据
    shopcarList() {
      return this.$store.state.list.filter((i) => i.count > 0);
    },
    //用计算属性筛选出  数量大于0的商品
  },
};
</script>
